package com.xgjweb.fsi.service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.xgjweb.common.dao.ICommonDao;
import com.xgjweb.common.service.CommonServiceImpl;
import com.xgjweb.common.utils.Constant;
import com.xgjweb.common.utils.XgjWebLogger;
import com.xgjweb.fsi.domain.Category;
import com.xgjweb.fsi.domain.Condition;
import com.xgjweb.fsi.domain.Journal;
import com.xgjweb.fsi.domain.Revert;
import com.xgjweb.fsi.domain.User;

/**
 * 
 * @author xgj
 * 
 */
public class JournalServiceImpl extends CommonServiceImpl implements
		IJournalService {
	private ICommonDao journalDao;

	public void setJournalDao(ICommonDao journalDao) {
		this.journalDao = journalDao;
	}

	/**
	 * 添加日志
	 */
	public Boolean addJournal(Journal journal) throws Exception {
		try {
			journal.setStatementName("insertJournal");
			journalDao.insert(journal);
			return true;
		} catch (RuntimeException e) {
			XgjWebLogger.getInstance().error(e.getMessage(), e);
			throw e;
		}
	}

	/**
	 * 更改日志
	 */
	public Boolean updateJournal(Journal journal) throws Exception {
		try {
			journal.setStatementName("updateJournal");
			journalDao.insert(journal);
			return true;
		} catch (RuntimeException e) {
			XgjWebLogger.getInstance().error(e.getMessage(), e);
			throw e;
		}
	}

	/**
	 * 删除日志
	 */
	public Boolean deleteJournal(Journal journal) throws Exception {
		try {
			journal.setStatementName("deleteJournal");
			journalDao.insert(journal);
			return true;
		} catch (RuntimeException e) {
			XgjWebLogger.getInstance().error(e.getMessage(), e);
			throw e;
		}
	}

	/**
	 * 添加类别
	 */
	public Boolean addCategory(Category category) throws Exception {
		try {
			category.setStatementName("insertCategory");
			journalDao.insert(category);
			return true;
		} catch (RuntimeException e) {
			XgjWebLogger.getInstance().error(e.getMessage(), e);
			throw e;
		}
	}

	/**
	 * 更改类别
	 */
	public Boolean updateCategory(Category category) throws Exception {
		try {
			category.setStatementName("updateCategory");
			journalDao.update(category);
			return true;
		} catch (RuntimeException e) {
			XgjWebLogger.getInstance().error(e.getMessage(), e);
			throw e;
		}
	}

	/**
	 * 删除类别
	 */
	public Boolean deleteCategory(Category category) throws Exception {
		try {
			category.setStatementName("deleteCategory");
			journalDao.delete(category);
			return true;
		} catch (RuntimeException e) {
			XgjWebLogger.getInstance().error(e.getMessage(), e);
			throw e;
		}
	}

	/**
	 * 查找类别
	 */
	public Category findCategory(Category category) throws Exception {
		try {
			category.setStatementName("findCategory");
			category = (Category) journalDao.selectByObject(category);
			return category;
		} catch (RuntimeException e) {
			XgjWebLogger.getInstance().error(e.getMessage(), e);
			throw e;
		}

	}

	/**
	 * 查找日志
	 */
	public Journal viewJournal(Journal journal) throws Exception {
		try {
			journal.setStatementName("selectJournalById");
			journal = (Journal) journalDao.selectByObject(journal);
			Revert revert = new Revert();
			revert.setJournal(journal.getId());
			revert.setStatementName("selectCountRevertByJournal");
			Integer dataCount = (Integer) journalDao.selectByObject(revert);
			revert.setStatementName("selectRevertByJournal");
			List<Revert> revertList = journalDao.selectByList(revert);
			if (dataCount > 0) {
				journal.getCondition().setDataCount(dataCount);
				User user;
				for (Revert revertTemp : revertList) {
					user = new User();
					if (revertTemp.getUser_id() != null) {
						user.setId(revertTemp.getUser_id());
						user.setStatementName("selectUserByID");
						user = (User) journalDao.selectByObject(user);
						String picture = user.getPicture();
						picture = Constant.PIC_PATH + picture;
						File file = new File(picture);
						if (!file.exists())
							user.setPicture(Constant.NO_PIC);
						revertTemp.setUser(user);
					} else {
						user.setName("匿名");
						user.setPicture("default.jpg");
						revertTemp.setUser(user);
					}
				}
				journal.setReverts(revertList);
			}
			return journal;
		} catch (Exception e) {
			XgjWebLogger.getInstance().error(e.getMessage(), e);
			throw e;
		}
	}

	/**
	 * 添加评论
	 */
	public Journal addRevert(Revert revert) throws Exception {
		try {

			revert.setCondition(new Condition());
			revert.setStatementName("insertRevert");
			journalDao.insert(revert);
			return queryRevert(revert);
		} catch (Exception e) {
			XgjWebLogger.getInstance().error(e.getMessage(), e);
			throw e;
		}
	}

	/**
	 * 删除评论
	 */
	public Boolean deleteRevert(Revert revert) throws Exception {
		try {
			revert.setStatementName("deleteRevert");
			journalDao.delete(revert);
			return true;
		} catch (Exception e) {
			XgjWebLogger.getInstance().error(e.getMessage(), e);
			throw e;
		}
	}

	/**
	 * 查找评论
	 */
	public Journal queryRevert(Revert revert) throws Exception {
		Journal journal = new Journal();
		journal.setCondition(revert.getCondition());
		revert.setStatementName("selectCountRevertByJournal");
		Integer dataCount = (Integer) journalDao.selectByObject(revert);
		revert.setStatementName("selectRevertByJournal");
		List<Revert> revertList = journalDao.selectByList(revert);
		if (revertList.size() > 0) {
			journal.getCondition().setDataCount(dataCount);
			User user;
			for (Revert revertTemp : revertList) {
				user = new User();
				if (revertTemp.getUser_id() != null) {
					user.setId(revertTemp.getUser_id());
					user.setStatementName("selectUserByID");
					user = (User) journalDao.selectByObject(user);
					String picture = user.getPicture();
					picture = Constant.PIC_PATH + picture;
					File file = new File(picture);
					if (!file.exists())
						user.setPicture(Constant.NO_PIC);
					revertTemp.setUser(user);
				} else {
					user.setName("匿名");
					user.setPicture(Constant.DEFAOULT_PIC);
					revertTemp.setUser(user);
				}
			}
		}
		journal.setReverts(revertList);
		return journal;
	}

	/**
	 * 得到所有的日志类别
	 */
	public List<Category> getCategorys() throws Exception {
		try {
			Category category = new Category();
			category.setStatementName("selectcCategory");
			return journalDao.selectByList(category);
		} catch (RuntimeException e) {
			XgjWebLogger.getInstance().error(e.getMessage(), e);
			throw e;
		}
	}

	/**
	 * 分页查找
	 */
	@SuppressWarnings("unchecked")
	public List<Journal> seletJournalByPage(Journal journal) throws Exception {
		try {
			journal.setStatementName("selectDataCount");
			Integer dataCount = (Integer) journalDao.selectByObject(journal);
			if (dataCount != 0) {
				journal.getCondition().setDataCount(dataCount);
				journal.setStatementName("selectjournalByPage");
				List<Journal> selectByList = (List<Journal>) journalDao
						.selectByList(journal);
				if (selectByList.size() > 0) {
					selectByList.get(0).setCondition(journal.getCondition());
					selectByList.get(0).getCondition().setDataCount(dataCount);
				}
				return selectByList;
			}
			return null;
		} catch (RuntimeException e) {
			XgjWebLogger.getInstance().error(e.getMessage(), e);
			throw e;
		}
	}

	/**
	 * 查询日志
	 */
	public List<Category> queryCategory() throws Exception {
		try {
			Category category = new Category();
			category.setStatementName("queryCategory");
			return journalDao.selectByList(category);
		} catch (RuntimeException e) {
			XgjWebLogger.getInstance().error(e.getMessage(), e);
			throw e;
		}
	}

}
